package com.bestv.search.management.command;

import com.bestv.search.common.dataSource.DataSourceKey;
import com.bestv.search.common.model.NetInfoHotComment;
import com.bestv.search.common.model.SysUser;
import com.bestv.search.common.service.NetInfoHotCommentManager;
import com.bestv.search.common.util.CommonUtil;
import com.bestv.search.common.util.StringConst;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;

import java.util.List;
import java.util.Map;

public class NetInfoHotCommentCommand {
    private static Logger logger = Logger.getLogger(NetInfoHotCommentCommand.class);
    private NetInfoHotCommentManager netInfoHotCommentManager;

    public String getHotComments(SysUser user, Map<String, String[]> map, DataSourceKey dsKey) throws JSONException {
        String[] start = map.get("start");
        if (start == null || start.length == 0 || start[0].equals(StringConst.EMPTY)) {
            logger.error("Parameter of start must exist, please provide.");
            return "[error]";
        }

        String[] limit = map.get("limit");
        if (limit == null || limit.length == 0 || limit[0].equals(StringConst.EMPTY)) {
            logger.error("Parameter of limit must exist, please provide.");
            return "[error]";
        }

        String[] netInfoIds = map.get("netInfoId");
        if (netInfoIds != null && netInfoIds.length > 0 && !netInfoIds[0].equals(StringConst.EMPTY)) {
            long netInfoId = Long.valueOf(netInfoIds[0]);

            int totalCount = netInfoHotCommentManager.getHotCommentCountByNetInfoId(dsKey, netInfoId);
            if (totalCount > 0) {
                List<NetInfoHotComment> list = netInfoHotCommentManager.getNetInfoHotCommentsByNetInfoId(dsKey,
                        netInfoId, Integer.valueOf(start[0]), Integer.valueOf(limit[0]));
                // Convert to JSON String
                if (list != null && list.size() > 0) {
                    JSONArray array = new JSONArray();
                    int index = 0;
                    for (NetInfoHotComment comment : list) {
                        JSONObject jo = new JSONObject();
                        jo.put("index", index);
                        jo.put("author", comment.getAuthorName());
                        jo.put("content", CommonUtil.convert(comment.getContent(), StringConst.UTF_8));
                        jo.put("published", comment.getPublished());
                        array.add(jo);
                        index++;
                    }
                    return "{'totalCount':" + totalCount + ", 'result':" + array.toString() + "}";
                } else {
                    return "{'totalCount':0, 'result':''}";
                }
            } else {
                return "{'totalCount':0, 'result':''}";
            }
        } else {
            logger.error("Parameter of netInfoId must exist, please provide.");
            return "[error]";
        }
    }

    public NetInfoHotCommentManager getNetInfoHotCommentManager() {
        return netInfoHotCommentManager;
    }

    public void setNetInfoHotCommentManager(NetInfoHotCommentManager netInfoHotCommentManager) {
        this.netInfoHotCommentManager = netInfoHotCommentManager;
    }
}